package com.ming.common.realm;

import com.ming.common.Constant;
import com.ming.domain.SysRole;
import com.ming.domain.SysUser;
import com.ming.mapper.SysPermissionMapper;
import com.ming.mapper.SysRoleMapper;
import com.ming.mapper.SysUserMapper;
import com.ming.query.SysRoleQuery;
import com.ming.vo.SysPermissionVO;
import com.ming.vo.SysRoleVO;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author:何元才
 * @Desc: 认证鉴权器
 */
@Component("authorizer")
public class AuthorRealm extends AuthorizingRealm {
    

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 认证方法
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken token1 = (UsernamePasswordToken) token;
        String username = token1.getUsername();
        String password = new String(token1.getPassword());
        // 登陆校验
        SysUser sysUser =  sysUserMapper.selectUserByNameAndPassword(username, password);
        if (sysUser == null || sysUser.getDeleted() == Constant.INVALID ){
            return null;
        }
        // 创建包装类
        ActiveUser activeUser = new ActiveUser();
        activeUser.setSysUser(sysUser);
        activeUser.setRealName(sysUser.getRealname());
        // 设置角色
        // 设置权限
        // 查询当前用户所有的角色和权限
        // 设置角色标签
        SysRoleQuery query = new SysRoleQuery();
        query.setUserId(sysUser.getId());
        List<SysRoleVO> sysRoleVOS = sysRoleMapper.selectList(query);
        //查询权限
        List<SysPermissionVO> sysPermissionVOS = sysPermissionMapper.queryUserPermission(sysUser.getId());
        //获取所有存储了角色标识的List
        List<String> roleTags = sysRoleVOS.parallelStream().map(SysRole::getTag).collect(Collectors.toList());
        //获取所有存储了权限标识的List
        List<String> permissionTags = sysPermissionVOS.parallelStream().map(SysPermissionVO::getTag).collect(Collectors.toList());
        //设置权限
        activeUser.setPermissions(permissionTags);
        activeUser.setRoles(roleTags);

        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(activeUser, sysUser.getLoginPassword(), sysUser.getRealname());
        return simpleAuthenticationInfo;

    }


    /**
     * 鉴权方法
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        ActiveUser activeUser = (ActiveUser) principals.getPrimaryPrincipal();
        //获取所有的角色
        List<String> roles = activeUser.getRoles();
        //获取所有的权限
        List<String> permissions = activeUser.getPermissions();
        //鉴权容器
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addRoles(roles);
        simpleAuthorizationInfo.addStringPermissions(permissions);

        return simpleAuthorizationInfo;
    }


//    public static void main(String[] args) {
//        Md5Hash 打工人 = new Md5Hash("12345", "打工人", 2);
//        System.out.println(打工人);
//    }



}
